package com.example.xgakj.newterminal.models;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.example.xgakj.mylibrary.mvp.utils.CellphoneInfo;
import com.example.xgakj.mylibrary.mvp.utils.CommonUtil;
import com.example.xgakj.mylibrary.mvp.utils.SharedPreferencesUtil;
import com.example.xgakj.mylibrary.mvp.utils.ToastUtil;
import com.example.xgakj.newterminal.base.BaseApplication;
import com.example.xgakj.newterminal.base.Parameters;
import com.example.xgakj.newterminal.beans.BaoAnBean;
import com.example.xgakj.newterminal.beans.ResultBean;
import com.example.xgakj.newterminal.pushmodule.OnlineService;
import com.example.xgakj.newterminal.ui.activities.LoginActivity;
import com.example.xgakj.newterminal.ui.activities.MainActivity;
import com.example.xgakj.newterminal.ui.activities.SplashActivity;
import com.example.xgakj.newterminal.utils.NetUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.socks.library.KLog;

import java.util.List;

/**
 * @author Administrator
 * @date 2017-10-16
 */

public class LoginModel {
    public void login(final String name, final String password, final Activity activity){
        String cpn= CellphoneInfo.getDeviceId(activity);
        OkGo.<ResultBean>post(BaseApplication.baseUrl)
                .isSpliceUrl(true)
                .params("cmd","login")
                .params("cpn",cpn)
                .params("usr",name)
                .params("pwd", CommonUtil.getMd5(password))
                .execute(new BaseJsonCallBack<ResultBean>(activity){
                    @Override
                    public void onSuccess(Response<ResultBean> response) {
                        ResultBean resultBean = response.body();

                        if ("OK".equals(resultBean.getRet())){
                            if (SharedPreferencesUtil.getBoolean(activity, Parameters.ALARM_NOTICE,true)){
                                Intent intent=new Intent(activity, OnlineService.class);
                                intent.putExtra("CMD", "RESET");
                                activity.startService(intent);
                            }
                            ToastUtil.showShort(activity,"登录成功");
                            //登录成功获取到ssn存入到BaseApplication中
                            BaseApplication.ssn=resultBean.getSsn();
                            BaseApplication.usrID=name;
                            queryBaoAn();

                            SharedPreferencesUtil.setString(activity, Parameters.USER_NAME,name);
                            SharedPreferencesUtil.setString(activity, Parameters.PASS_WORD,password);
                            Intent intent=new Intent(activity, MainActivity.class);
                            activity.startActivity(intent);
                            activity.finish();

                        }else {
                            ToastUtil.showShort(activity,"账号或者密码错误");
                            //如果是splash界面登录失败，则跳转到Login界面
                            if (activity instanceof SplashActivity){
                                Intent intent=new Intent(activity, LoginActivity.class);
                                activity.startActivity(intent);
                                activity.finish();
                            }
                        }
                        KLog.d(resultBean.toString());
                    }

                    @Override
                    public void onError(Response<ResultBean> response) {
                        super.onError(response);
                        //如果是splash界面登录失败，则跳转到Login界面
                        if (activity instanceof SplashActivity){
                            Intent intent=new Intent(activity, LoginActivity.class);
                            activity.startActivity(intent);
                            activity.finish();
                        }
                    }
                });

    }
    public void loginOut(Context context){
            OkGo.<String>post(BaseApplication.baseUrl)
                    .isSpliceUrl(true)
                    .params("cmd","logout")
                    .params("ssn",BaseApplication.ssn)
                    .params("usr", SharedPreferencesUtil.getString(context,Parameters.USER_NAME,""))
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            KLog.d(response.body().toString());
                        }
                    });

    }
    public void queryBaoAn(){

        String queryUsr="ID="+"'"+ BaseApplication.usrID+"'";
        String[] keys={"table","field","param"};
        String[] values={Parameters.T_SEC,"*",queryUsr};
        String json = NetUtil.toJSON(keys, values);
        OkGo.<List<BaoAnBean>>post(BaseApplication.baseUrl)
                .isSpliceUrl(true)
                .params("cmd","select")
                .params("ssn",BaseApplication.ssn)
                .params("usr",BaseApplication.usrID)
                .upString(json)
                .execute(new BaseJsonCallBack<List<BaoAnBean>>(null) {
                    @Override
                    public void onSuccess(Response<List<BaoAnBean>> response) {
                        List<BaoAnBean> list = response.body();
                        if (list!=null&&list.size()>0){
                            BaoAnBean baoAnBean=list.get(0);
                            analyseBean(baoAnBean);
                            KLog.e(baoAnBean.toString());

                        }



                    }
                });

    }

    /**
     * 分析处理用户参数
     * @param baoAnBean 用户bean
     */
    private void analyseBean(BaoAnBean baoAnBean) {
        BaseApplication.mBaoAnBean=baoAnBean;
        BaseApplication.name=baoAnBean.getName();
        int flag = baoAnBean.getFlag();
        BaseApplication.flag=flag;
        String class_value = baoAnBean.getClass2();
        if(flag==1)
        {
            BaseApplication.classKey="PartID";
            if(class_value.length()>=6)
            {
                BaseApplication.classValue=class_value.substring(0, 6);
            }
            else
            {
                BaseApplication.classValue="00000/0";
            }
        }
        else if(flag==2)
        {
            BaseApplication.classKey="chengquid";
            if(class_value.length()>=5)
            {
                BaseApplication.classValue=class_value.substring(0, 5);
            }
            else
            {
                BaseApplication.classValue="00000/0";
            }
        }
        else if(flag==3)
        {
            BaseApplication.classKey="chengquid";
            if(class_value.length()>=5)
            {
                BaseApplication.classValue=class_value.substring(0, 5);
            }
            else
            {
                BaseApplication.classValue="00000/0";
            }
        }
    }
}
